import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/config.dart';
import 'package:finance_app/src/widgets/bottom_sheet/default_bottom_sheet.dart';
import 'package:finance_app/src/widgets/button/normal_button.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:r_datetime_picker/r_datetime_picker.dart';

showYearMonthSelectBottomSheet(
    BuildContext context, String title, DateTime select) async {
  DateTime currentSelect = select.add(Duration(seconds: 1)) ?? DateTime.now();
  return await showModalBottomSheet(
      context: context,
      isScrollControlled: true,
      backgroundColor: Colors.transparent,
      builder: (BuildContext context) => DefaultBottomSheet(
            title: title,
            child: YearMonthSelectBottomSheet(
              select: currentSelect,
              dateTimeCall: (dataTime) {
                currentSelect = dataTime;
              },
              onDone: () {
                Navigator.of(context).pop(currentSelect);
              },
            ),
          ));
}

class YearMonthSelectBottomSheet extends StatelessWidget {
  final ValueChanged<DateTime> dateTimeCall;
  final VoidCallback onDone;
  final DateTime select;
  const YearMonthSelectBottomSheet(
      {Key key, this.dateTimeCall, this.onDone, this.select})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        RDateTimePicker(
          initialDate: select ?? DateTime.now(),
          onChangedDate: _onChangeDateTime,
          maxDate:
              DateTime.tryParse(Config.currentCompany?.financialYearEndAt) ??
                  DateTime(2050, 12, 31),
          minDate:
              DateTime.tryParse(Config.currentCompany?.financialYearStartAt) ??
                  DateTime(1900, 1, 1),
          hideDays: true,
          textColor: aceDarkGrayColor50,
          selectTextStyle: TextStyle(
            color: aceBlueColor,
            fontSize: getSp(18),
            fontWeight: FontWeight.w700,
            height: 22 / 18,
          ),
          unSelectTextStyle: TextStyle(
            color: aceDarkGrayColor50,
            fontSize: getSp(18),
            fontWeight: FontWeight.w400,
            height: 22 / 18,
          ),
          foregroundWidget: Container(
            height: getDp(42),
            decoration: BoxDecoration(
                border: Border.all(
              color: aceGrayColor,
              width: getDp(0.5),
            )),
          ),
        ),
        NormalButton(
          title: S.current.done,
          onTap: onDone,
        ),
      ],
    );
  }

  void _onChangeDateTime(DateTime value) {
    dateTimeCall?.call(value);
  }
}
